package cn.xianyang;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class EsTest {

    @Test
    public void testAdd(){
        //获取客户端ES
        TransportClient client = ESClientUtil.getClient();
        //创建索引文档
        IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "1");
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("id",1L);
        map.put("name","大行星");
        map.put("age",11L);
        map.put("sex",1);
        IndexResponse response = builder.setSource(map).get();
        System.out.println(response);

    }

    @Test
    public void testGet(){
        //获取客户端ES
        TransportClient client = ESClientUtil.getClient();
        //索引，类型，id查询对象
        GetRequestBuilder builder = client.prepareGet("pethome", "pet", "1");
        GetResponse response = builder.get();
        System.out.println(response);
        System.out.println(response.getSourceAsMap());
        System.out.println(response.getSource());

    }

    @Test
    public void testUpdate(){
        //获取客户端ES
        TransportClient client = ESClientUtil.getClient();
        //创建索引文档
        UpdateRequestBuilder builder = client.prepareUpdate("pethome", "pet", "1");
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("name","大天狼星");
        map.put("age",2200);
        System.out.println(builder.setDoc(map).get());

    }

    @Test
    public void testDelete(){
        //获取客户端
        TransportClient client = ESClientUtil.getClient();
        //创建索引文档
        DeleteRequestBuilder builder = client.prepareDelete("pethome", "pet", "1");
        System.out.println(builder.get());
    }

    /**
     * 设置数据
     */
    @Test
    public void testFor(){
        //获取客户端
        TransportClient client = ESClientUtil.getClient();
        for (int i = 1;i<100;i++){
            //创建索引文档
            IndexRequestBuilder builder = client.prepareIndex("pethome", "pet", "1");
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("id",i -1);
            //map.put("name",i%3==1?"王大锤"+i:"王小锤"+i);
            map.put("name","王大锤是王小锤的爸爸!"+i);
            map.put("age",i -1);
            //map.put("sex",i%2==0?1:0);
            map.put("sex",i%2);
            IndexResponse response = builder.setSource(map).get();
            System.out.println(response);
        }

        client.close();
    }


    /**
     * 构建综合查询
     */
    @Test
    public void testSearch(){
        //1.获取客户端Es
        TransportClient client = ESClientUtil.getClient();
        //2.设置要查询的索引
        SearchRequestBuilder builder = client.prepareSearch("pethome");
        /*builder.setTypes("pet");*/
        //3.构建查询条件
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //设置组合查询中的查询条件
        boolQuery.must(QueryBuilders.matchQuery("name","小锤"));
        //过滤条件
        boolQuery.filter(QueryBuilders.termQuery("sex",1))
                .filter(QueryBuilders.rangeQuery("age").gte(2L).lte(90L));
        //4.设置分页排序 （页码-1）*每页展示条数
        builder.setFrom(1);
        builder.setSize(2);
        //根据年龄 倒叙排序
        builder.addSort("age", SortOrder.DESC);

        //设置条件为组合查询
        /*builder.setQuery(boolQuery);*/
        //获取条件查询的结果
        SearchResponse response = builder.get();

        //总命中的结果
        SearchHits hits = response.getHits();
        System.out.println("总命中数:"+hits.getTotalHits());
        //当前页的数据
        SearchHit[] currentList = hits.getHits();
        for (SearchHit row : currentList) {
            System.out.println(row.getSourceAsMap());
        }
        

    }











}
